package leetcode.sort;

/**
选择排序，在未排序数组中找到最小的，放在已排序的最后面
 */
public class SelectSort {
    public static void main(String[] args) {
        int [] arr = new int[]{1,3,2,5,8,5,7,4,2,10,19,13,12};
        new SelectSort().selectSort(arr);
        for (int i : arr) {
            System.out.print(i+" ");
        }
    }

    private void selectSort(int []nums){
        for (int i = 0; i < nums.length; i++) {
            int min = i;
            for (int j = i+1 ; j < nums.length ; j++) {
                    if(nums[j]<nums[min]){
                        min = j;
                    }
            }
            swap(nums,min,i);
        }
    }

    private void swap(int []nums,int i,int j){
        int t = nums[i];
        nums[i] = nums[j];
        nums[j] = t;
    }
}